#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int count = 0;
        for (int max = nums.size() - 1; max >= 2; max--)
        {
            int left = 0, right = max - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] <= nums[max])
                {
                    left++;
                }
                else if (nums[left] + nums[right] > nums[max])
                    count += right - left,
                    right--;
            }
        }
        return count;
    }
};